//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssTranReceiveDao;
import com.aidong.dao.wills.PssTranSendListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import com.aidong.model.wills.PssTranReceive;
import com.aidong.model.wills.PssTranSend;
import com.aidong.model.wills.PssTranSendImpl;
import com.aidong.model.wills.PssTranSendList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssTranSendDao extends BaseDao<PssTranSend, Integer> {
    public PssTranSendDao() {
    }

    public void addTranSend(PssTranSend pssTranSend, PssProductStockDao pssProductStockDao, List<PssTranSendList> pssTranSendList) {
        this.add(pssTranSend);
        PssTranReceive pssTranReceive = null;
        Iterator i$ = pssTranSendList.iterator();

        while(i$.hasNext()) {
            PssTranSendList pss = (PssTranSendList)i$.next();
            pss.setSendID(pssTranSend.getSendID());
            this.getSession().save(pss);
            pssTranReceive = new PssTranReceive();
            pssTranReceive.setSendID(pssTranSend.getSendID());
            pssTranReceive.setReceiveDate(new Date());
            pssTranReceive.setCreateuser(pssTranSend.getCreateuser());
            pssTranReceive.setCreatetime(new Date());
            pssTranReceive.setUpdatetime(new Date());
            pssTranReceive.setProductCode(pss.getProductCode());
            pssTranReceive.setQuantity(pss.getQuantity());
            this.getSession().save(pssTranReceive);
            PssProductStockPK sendPK = new PssProductStockPK();
            sendPK.setProductCode(pss.getProductCode());
            sendPK.setShopCode(pssTranSend.getSendShop());
            PssProductStock sendStock = (PssProductStock)pssProductStockDao.getById(sendPK);
            if(sendStock == null) {
                sendStock = new PssProductStock();
                sendStock.setProductCode(pss.getProductCode());
                sendStock.setShopCode(pssTranSend.getSendShop());
                sendStock.setQuantity(Double.valueOf(-pss.getQuantity().doubleValue()));
                sendStock.setCreateUser(pssTranSend.getCreateuser());
                sendStock.setCreateTime(new Date());
                sendStock.setUpdateTime(new Date());
                pssProductStockDao.add(sendStock);
            } else {
                sendStock.setQuantity(Double.valueOf(sendStock.getQuantity().doubleValue() - pss.getQuantity().doubleValue()));
                sendStock.setUpdateTime(new Date());
                pssProductStockDao.update(sendStock);
            }

            PssProductStockPK receivePK = new PssProductStockPK();
            receivePK.setProductCode(pss.getProductCode());
            receivePK.setShopCode(pssTranSend.getReceiveShop());
            PssProductStock receiveStock = (PssProductStock)pssProductStockDao.getById(receivePK);
            if(receiveStock == null) {
                receiveStock = new PssProductStock();
                receiveStock.setProductCode(pss.getProductCode());
                receiveStock.setShopCode(pssTranSend.getReceiveShop());
                receiveStock.setQuantity(pss.getQuantity());
                receiveStock.setCreateUser(pssTranSend.getCreateuser());
                receiveStock.setCreateTime(new Date());
                receiveStock.setUpdateTime(new Date());
                pssProductStockDao.add(receiveStock);
            } else {
                receiveStock.setQuantity(Double.valueOf(receiveStock.getQuantity().doubleValue() + pss.getQuantity().doubleValue()));
                receiveStock.setUpdateTime(new Date());
                pssProductStockDao.update(receiveStock);
            }
        }

    }

    public void updateTranSend(PssTranSend pssTranSend, PssTranSendListDao pssTranSendListDao, PssProductStockDao pssProductStockDao, PssTranReceiveDao pssTranReceiveDao, Products products) {
        Iterator i$ = products.getLstProduct().iterator();

        while(i$.hasNext()) {
            PssPopProduct pc = (PssPopProduct)i$.next();
            PssTranSendList pssTranSendList = (PssTranSendList)pssTranSendListDao.getById(pc.getId());
            double oldQty = pssTranSendList.getQuantity().doubleValue();
            if(Double.compare(pc.getQty().doubleValue(), pssTranSendList.getQuantity().doubleValue()) != 0) {
                pssTranSendList.setQuantity(pc.getQty());
                pssTranSendListDao.update(pssTranSendList);
                PssTranReceive pssTranReceive = (PssTranReceive)pssTranReceiveDao.getById(pc.getId());
                pssTranReceive.setQuantity(pc.getQty());
                pssTranReceiveDao.update(pssTranReceive);
                PssProductStockPK sendPK = new PssProductStockPK();
                sendPK.setProductCode(pc.getProductCode());
                sendPK.setShopCode(pssTranSend.getSendShop());
                PssProductStock sendStock = (PssProductStock)pssProductStockDao.getById(sendPK);
                if(sendStock == null) {
                    sendStock = new PssProductStock();
                    sendStock.setProductCode(pc.getProductCode());
                    sendStock.setShopCode(pssTranSend.getSendShop());
                    sendStock.setQuantity(Double.valueOf(-(pc.getQty().doubleValue() - oldQty)));
                    sendStock.setCreateUser(pssTranSend.getCreateuser());
                    sendStock.setCreateTime(new Date());
                    sendStock.setUpdateTime(new Date());
                    pssProductStockDao.add(sendStock);
                } else {
                    sendStock.setQuantity(Double.valueOf(sendStock.getQuantity().doubleValue() - (pc.getQty().doubleValue() - oldQty)));
                    sendStock.setUpdateTime(new Date());
                    pssProductStockDao.update(sendStock);
                }

                PssProductStockPK receivePK = new PssProductStockPK();
                receivePK.setProductCode(pc.getProductCode());
                receivePK.setShopCode(pssTranSend.getReceiveShop());
                PssProductStock receiveStock = (PssProductStock)pssProductStockDao.getById(receivePK);
                if(receiveStock == null) {
                    receiveStock = new PssProductStock();
                    receiveStock.setProductCode(pc.getProductCode());
                    receiveStock.setShopCode(pssTranSend.getReceiveShop());
                    receiveStock.setQuantity(pc.getQty());
                    receiveStock.setCreateUser(pssTranSend.getCreateuser());
                    receiveStock.setCreateTime(new Date());
                    receiveStock.setUpdateTime(new Date());
                    pssProductStockDao.add(receiveStock);
                } else {
                    receiveStock.setQuantity(Double.valueOf(receiveStock.getQuantity().doubleValue() + (pc.getQty().doubleValue() - oldQty)));
                    receiveStock.setUpdateTime(new Date());
                    pssProductStockDao.update(receiveStock);
                }
            }
        }

    }

    public void getTranSendRptList(Pagination<PssTranSendImpl> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select pr.SendID, pr.SendShop, pr.SendDate,pr.ReceiveShop,pr.createuser, prl.ProductCode, pc.Unit, sum(prl.quantity) as quantity \n from PssTranSend pr \n inner join PssTranSendList prl on pr.SendID=prl.SendID \n left outer  join PssProductCode pc on prl.productcode=pc.ProductCode  \n" + (sqlw.length() > 0?"where " + sqlw:"") + " group by pr.SendID, pr.SendShop, pr.SendDate,pr.ReceiveShop,pr.createuser, prl.ProductCode, pc.Unit \n";
        sql = sql + " order by pr.SendID, pr.ReceiveShop, prl.ProductCode ";
        this.queryPageListBySql(PssTranSendImpl.class, pagination, sql, params.toArray());
    }

    public List<PssTranSendImpl> getExcelTranSendRptList(ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select pr.SendID, pr.SendShop, c.ChName as sendShopName, pr.SendDate,pr.ReceiveShop, d.ChName as receiveShopName, e.chName as createuser, prl.ProductCode, pc.ProductName, pc.Unit, sum(prl.quantity) as quantity \n from PssTranSend pr \n inner join PssTranSendList prl on pr.SendID=prl.SendID \n left outer  join PssProductCode pc on prl.productcode=pc.ProductCode  \n left outer  join DicShopCode c on pr.SendShop=c.Code  \n left outer  join DicShopCode d on pr.ReceiveShop=d.Code  \n left outer  join SysUser e on pr.createuser=e.userCode  \n" + (sqlw.length() > 0?"where " + sqlw:"") + " group by pr.SendID, pr.SendShop,c.ChName, pr.SendDate,pr.ReceiveShop,d.ChName, e.chName, prl.ProductCode,pc.ProductName, pc.Unit \n";
        sql = sql + " order by pr.SendID, pr.ReceiveShop, prl.ProductCode ";
        return this.queryListBySql(PssTranSendImpl.class, sql, params.toArray());
    }
}
